package com.cy.law.task;

import com.cy.law.db.dao.HKUnitDao;
import com.cy.law.db.dao.HkDeviceDao;
import com.cy.law.db.entity.HkDevice;
import com.cy.law.db.entity.HkUnit;
import com.cy.law.task.device.vo.DeviceResponse;
import com.cy.law.task.unit.vo.UnitResponse;
import com.cy.law.utils.IHttpUtil;
import com.cy.law.utils.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
public class HKSyncTask {

    @Autowired
    HKUnitDao hkUnitDao;
    @Autowired
    HkDeviceDao hkDeviceDao;

    private final static Integer PAGE_SIZE = 20;

    /**
     * 同步组织信息
     * 每天凌晨三点执行
     */
    @Scheduled(cron = "0 0 3 * * ? ")
    public void unit() {
        // 根据第一次的接口返回信息,获取接下来还需要再同步几次
        try {
            String result = IHttpUtil.getUnit(0, PAGE_SIZE);
            UnitResponse unitResponse = JSONUtil.toObject(result, UnitResponse.class);
            if (unitResponse == null) {
                log.error("第一次同步组织失败,请检查接口!");
                return;
            }
            if (unitResponse.getData() != null) {
                hkUnitDao.saveAll(transUnit(unitResponse.getData()));
            }
            int index = 1;
            int totalPageIndex = getTotalPageIndex(unitResponse.getPage().getTotal());
            for (index = 1; index <= totalPageIndex; index++) {
                result = IHttpUtil.getUnit(index, PAGE_SIZE);
                UnitResponse tmp = JSONUtil.toObject(result, UnitResponse.class);
                if (tmp != null && tmp.getData() != null) {
                    hkUnitDao.saveAll(transUnit(tmp.getData()));
                }
            }

        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }


    /**
     * 同步监控点信息
     * 每天凌晨四点执行
     */
    @Scheduled(cron = "0 0 4 * * ? ")
    public void device() {
        // 根据第一次的接口返回信息,获取接下来还需要再同步几次
        try {
            String result = IHttpUtil.getDevice(0, PAGE_SIZE);
            DeviceResponse deviceResponse = JSONUtil.toObject(result, DeviceResponse.class);
            if (deviceResponse == null) {
                log.error("第一次同步监控点失败,请检查接口!");
                return;
            }
            if (deviceResponse.getData() != null) {
                hkDeviceDao.saveAll(transDevice(deviceResponse.getData()));
            }
            int index = 1;
            int totalPageIndex = getTotalPageIndex(deviceResponse.getPage().getTotal());
            for (index = 1; index <= totalPageIndex; index++) {
                result = IHttpUtil.getDevice(index, PAGE_SIZE);
                DeviceResponse tmp = JSONUtil.toObject(result, DeviceResponse.class);
                if (tmp != null && tmp.getData() != null) {
                    hkDeviceDao.saveAll(transDevice(tmp.getData()));
                }
            }

        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    private List<HkDevice> transDevice(List<com.cy.law.task.device.vo.DataItem> old) {
        List<HkDevice> list = new ArrayList<>();
        if (old != null) {
            old.forEach(t -> {
                HkDevice tmp = new HkDevice();
                BeanUtils.copyProperties(t, tmp);
                list.add(tmp);
            });
        }
        return list;
    }

    private List<HkUnit> transUnit(List<com.cy.law.task.unit.vo.DataItem> old) {
        List<HkUnit> list = new ArrayList<>();
        if (old != null) {
            old.forEach(t -> {
                HkUnit tmp = new HkUnit();
                BeanUtils.copyProperties(t, tmp);
                list.add(tmp);
            });
        }
        return list;
    }

    private int getTotalPageIndex(int total) {
        // pageIndex 从0开始
        if (total % PAGE_SIZE == 0) {
            return (total / PAGE_SIZE) - 1;
        } else {
            return total / PAGE_SIZE;
        }
    }
}
